#ifndef __ZHONG_DMSER__
#define __ZHONG_DMSER__
#include "logger.hpp"
#include <cstdlib>
#include <iostream>
#include <string>
#include <memory>
#include <alibabacloud/core/AlibabaCloud.h>
#include <alibabacloud/core/CommonRequest.h>
#include <alibabacloud/core/CommonClient.h>
#include <alibabacloud/core/CommonResponse.h>
// namespace zhong
// {
//     // 对DMS的短信通知类与接口进行二次封装
//     namespace dmser
//     {

//     }
// }
namespace bite_im
{
    class DMSClient
    {
    public:
        using ptr = std::shared_ptr<DMSClient>;
        DMSClient(const std::string &access_key_id, const std::string &access_key_secret)
        {
            AlibabaCloud::InitializeSdk();
            AlibabaCloud::ClientConfiguration configuration("cn-hangzhou");
            // specify timeout when create client.
            configuration.setConnectTimeout(1500);
            configuration.setReadTimeout(4000);
            // Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
            AlibabaCloud::Credentials credential(access_key_id, access_key_secret);
            _client = std::make_unique<AlibabaCloud::CommonClient>(credential, configuration);
        }
        ~DMSClient()
        {
            AlibabaCloud::ShutdownSdk();
        }
        // 传入发送的验证码和目标手机号
        bool sendCode(const std::string &phone, const std::string &code)
        {
            AlibabaCloud::CommonRequest request(AlibabaCloud::CommonRequest::RequestPattern::RpcPattern);
            request.setHttpMethod(AlibabaCloud::HttpRequest::Method::Post);
            request.setDomain("dysmsapi.aliyuncs.com");
            request.setVersion("2017-05-25");
            request.setQueryParameter("Action", "SendSms");
            request.setQueryParameter("SignName", "钟的CharRoom");
            request.setQueryParameter("TemplateCode", "SMS_474010193");
            request.setQueryParameter("PhoneNumbers", phone);
            std::string code_str = "{\"code\":\"" + code + "\"}";
            request.setQueryParameter("TemplateParam", code_str);

            auto response = _client->commonResponse(request);
            if (!response.isSuccess())
            {
                LOG_ERROR("短信验证码请求失败，原因[{}]", response.error().errorMessage());
                return false;
            }
            return true;
        }

    private:
        std::unique_ptr<AlibabaCloud::CommonClient> _client;
    };
}

#endif